package ch.unisi.inf.pfii.teamred.pacman.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

/**
 * @author luca.vignola@lu.unisi.ch
 * 
 */
public abstract class Controller {

	private final Random random;
	private final ArrayList<Direction> directions;

	private HashMap<Direction, ArrayList<Block>> neighbors;
	private Position playerControlledCreaturePosition;
	private Position aiControlledCreaturePosition;
	private Direction aiControlledCreatureDirection;
	private boolean aiControlledCreatureVulnerable;

	public Controller() {
		random = new Random();
		directions = new ArrayList<Direction>();
		fillDirections();
	}

	public final Random getRandom() {
		return random;
	}

	public final Position getPlayerControlledCreaturePosition() {
		return playerControlledCreaturePosition;
	}

	public final Position getAiControlledCreaturePosition() {
		return aiControlledCreaturePosition;
	}

	public final boolean isAiControlledCreatureVulnerable() {
		return aiControlledCreatureVulnerable;
	}

	public final void setUp(
			final HashMap<Direction, ArrayList<Block>> neighbors,
			final Position playerControlledCreaturePosition,
			final Position aiControlledCreaturePosition,
			final Direction aiControlledCreatureDirection,
			final boolean aiControlledCreatureVulnerable) {
		this.neighbors = neighbors;
		this.playerControlledCreaturePosition = playerControlledCreaturePosition;
		this.aiControlledCreaturePosition = aiControlledCreaturePosition;
		this.aiControlledCreatureDirection = aiControlledCreatureDirection;
		this.aiControlledCreatureVulnerable = aiControlledCreatureVulnerable;
	}

	public final void setNeighbors(
			final HashMap<Direction, ArrayList<Block>> neighbors) {
		this.neighbors = neighbors;
	}

	public final HashMap<Direction, ArrayList<Block>> getNeighbors() {
		return neighbors;
	}

	public final void setCurrentDirection(
			final Direction aiControlledCreatureDirection) {
		this.aiControlledCreatureDirection = aiControlledCreatureDirection;
	}

	public final Direction getAiControlledCreatureDirection() {
		return aiControlledCreatureDirection;
	}

	public final ArrayList<Direction> getDirections() {
		return directions;
	}

	protected final boolean canGoStraight(final Direction direction) {
		if (neighbors.get(direction).get(0) != null
				&& neighbors.get(direction).get(0).isTraversable()) {
			return true;
		} else {
			return false;
		}
	}

	private final void fillDirections() {
		directions.add(Direction.UP);
		directions.add(Direction.DOWN);
		directions.add(Direction.LEFT);
		directions.add(Direction.RIGHT);
	}

	abstract public Direction move();
}
